package com.tanovo.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tanovo.bo.OauthClientDetailsBo;
import com.tanovo.bo.ResultObject;
import com.tanovo.bo.SwithcDataInfoBo;
import com.tanovo.bo.UserCore;
import com.tanovo.config.UserInfoContext;
import com.tanovo.domain.OauthClientDetails;
import com.tanovo.service.IOauthClientDetailsService;
import com.tanovo.service.IOauthClientTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "客户端")
public class OauthClientDetailsApi {
    @Autowired
    private IOauthClientDetailsService oauthClientDetailsService;


    @PostMapping("/client/add")
    @ApiOperation(value = "添加应用", notes = "添加应用")
    public ResultObject addClient(@RequestBody OauthClientDetailsBo oauthClientDetails) {
        oauthClientDetails.setCompanyId(UserInfoContext.getUser().getCompanyId());
        oauthClientDetails.setUserId(UserInfoContext.getUser().getId());
        oauthClientDetails.setCreateBy(UserInfoContext.getUser().getId().toString());
        oauthClientDetails.setUpdateBy(UserInfoContext.getUser().getId().toString());
        oauthClientDetailsService.addClientAndData(oauthClientDetails);
        return new ResultObject();
    }

    @PostMapping("/client/delete")
    @ApiOperation(value = "删除应用", notes = "删除应用")
    public ResultObject deleteCient(@RequestBody List<Integer> idList) {
        oauthClientDetailsService.deleteCientIdList(idList);
        return new ResultObject();
    }

    @PostMapping("/client/update")
    @ApiOperation(value = "更新应用", notes = "更新应用")
    public ResultObject updateCient(@RequestBody OauthClientDetails oauthClientDetails) {
        oauthClientDetailsService.updateCient(oauthClientDetails);
        return new ResultObject();
    }

    @PostMapping("/client/index/list")
    @ApiOperation(value = "展示首页的应用列表（无权限）", notes = "展示首页的应用列表（无权限）")
    public ResultObject<List<OauthClientDetails>> getIndexClientList() {
        List<OauthClientDetails> cientList = oauthClientDetailsService.getIndexClientList();
        return new ResultObject(cientList);
    }

    @PostMapping("/client/list")
    @ApiOperation(value = "获取当前登录公司绑定的应用列表", notes = "获取当前登录公司绑定的应用列表")
    public ResultObject<PageInfo<OauthClientDetails>> getCientList(@RequestBody OauthClientDetails oauthClientDetails) {
        PageHelper.startPage(oauthClientDetails.getPageNum(), oauthClientDetails.getPageSize());
        oauthClientDetails.setCompanyId(UserInfoContext.getUser().getCompanyId());
        List<OauthClientDetails> cientList = oauthClientDetailsService.getCompanyCientList(oauthClientDetails);
        PageInfo<OauthClientDetails> detailsPageInfo = new PageInfo<>(cientList);
        return new ResultObject(detailsPageInfo);
    }


    @GetMapping("/client/current/list")
    @ApiOperation(value = "当前公司登录人开通的应用列表", notes = "当前公司登录人开通的应用列表")
    public ResultObject<List<OauthClientDetails>> getCurrentCientList() {
        UserCore user = UserInfoContext.getUser();
        List<OauthClientDetails> cientList = oauthClientDetailsService.getCurrentCientList(user.getId(), user.getCompanyId());
        return new ResultObject(cientList);
    }

    @GetMapping("/client/openFlag")
    @ApiOperation(value = "判断当前登录人有没有开通应用", notes = "判断当前登录人有没有开通应用")
    public ResultObject<Boolean> openFlag(@RequestParam String clientId) {
        Boolean openFlag = oauthClientDetailsService.openFlag(clientId);
        return new ResultObject(openFlag);
    }

    @GetMapping("/client/openSubmit")
    @ApiOperation(value = "当前登录人发起开通客户端申请", notes = "当前登录人发起开通客户端申请")
    public ResultObject openClientSubmit(@RequestParam String clientId) {
        UserCore user = UserInfoContext.getUser();
        user.setClientId(clientId);
        oauthClientDetailsService.openClientSubmit(user);
        return new ResultObject();
    }


}
